package com.maduixa.guitar.tabsearch.spring.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import com.maduixa.guitar.tabsearch.spring.business.model.GuitarUser;
import com.maduixa.guitar.tabsearch.spring.business.model.Song;
import com.maduixa.guitar.tabsearch.spring.business.model.UserSong;
import com.maduixa.guitar.tabsearch.spring.data.DataController;
import com.maduixa.guitar.tabsearch.spring.to.ListUserSongTO;
import com.maduixa.guitar.tabsearch.spring.to.ListUsersTO;


public class UsersController{

    private DataController dB;

    public UsersController(DataController dB) {
		super();
		this.dB = dB;
	}
    
    public GuitarUser addUser(String uName, String pWord, String mail, boolean adm) {    
    	GuitarUser u = new GuitarUser(uName, pWord, mail, adm);
        dB.addUser(u);
        return u;
    }
    
    public GuitarUser updateUser(String uName, String pWord, String mail) {    
    	GuitarUser u = dB.getUser(uName);
    	if (pWord != null) u.setPasswordStr(pWord);
    	if (mail != null) u.setMail(mail);
        dB.updateUser(u);
        return u;
    }
    
	public GuitarUser getUser(String uName) {
		return dB.getUser(uName);			
	}

    public List<ListUsersTO> getAllUsers() {
    	List<ListUsersTO> users = new ArrayList<ListUsersTO>();
		List<Object> select = dB.getAllUsersByLevel();
		
		//comprovar l'usuari i afegir les 
		for (int i = 0; i < select.size(); i++) {
        	Object[] o = (Object[])select.get(i);
        	ListUsersTO lsto= getUsuari(users,o[0]);
        	if (lsto == null) {
        		lsto = new ListUsersTO((String)o[0], (String) o[1], (Boolean) o[2], (Date) o[3]);
        		users.add(lsto);
        	}
        	
        	if (o[5] == null) o[5] = 0;
        	
        	switch ((Integer) o[5]) {
			case 0:
				lsto.setLevel0((Long) o[4]);
				break;
			case 1:
				lsto.setLevel1((Long) o[4]);
				break;
			case 2:
				lsto.setLevel2((Long) o[4]);
				break;
			case 3:
				lsto.setLevel3((Long) o[4]);
				break;
			case 4:
				lsto.setLevel4((Long) o[4]);
				break;
			case 5:
				lsto.setLevel5((Long) o[4]);
				break;
			default:
				lsto.setLevel0((Long) o[4]);
				break;
			}
        	
		}
		
		return users;
    }
	
    private ListUsersTO getUsuari(List<ListUsersTO> users, Object object) {
		for (int i = 0; i < users.size(); i++) {
			if (users.get(i).getUsername().equals(object)) 
				return users.get(i);
		}
		return null;
	}

	public boolean loginUser(String uName, String pWord){ 
    	GuitarUser u = dB.getUser(uName);
    	
    	if(u == null) return false;
    	
    	return u.login(pWord);
    }

	public String validateLogin(String userName, String password) {
    	if(userName.isEmpty()) return "11";
    	if(password.isEmpty()) return "13";
    	boolean b = loginUser(userName, password);
    	if(!b) 	return "14";
    	return "10";
	}
	
	public UserSong addUserSong(String userId, long songId, String record, String cover, Integer level) {
		
		Song s = dB.getSong(songId);
		
		UserSong us = new UserSong(userId, s, record, cover, level);
		dB.addUserSong(us);
		
		return us;			
	}
	
	public UserSong updateUserSong(long id, String record, String cover, Integer level) {

		UserSong us = dB.getUserSong(id);
		
		us.setRecord(record);
		us.setCover(cover);
		us.setLevel(level);
		
		dB.updateUserSong(us);
		
		return us;			
	}
	
	public void removeMySong(long id) {
		dB.removeMySong(id);
	}
	
	public UserSong getUserSong(long id) {
		return dB.getUserSong(id);			
	}
	
	public List<ListUserSongTO> getMySongs(String uName, String order, String direction) {
		List<ListUserSongTO> songs = new ArrayList<ListUserSongTO>();
		List<Object> select = dB.getMySongs(uName, order, direction);
		
		for (int i = 0; i < select.size(); i++) {
        	Object[] o = (Object[])select.get(i);
        	ListUserSongTO lsto = new ListUserSongTO(((Long)o[0]), (String) o[1], (String) o[2], 
        			(String) o[3], (String) o[4], (String) o[5], (Integer) o[6]);
        	songs.add(lsto);
		}
		
		return songs;			
	}

	public UserSong getMySong(long id) {
		return dB.getUserSong(id);
	}

}
